import sqlite3

sqlite_connect_handle = sqlite3.connect("cq24_rpmpackagename_relevance.db")
sqlite_connect_handle_relevance = sqlite3.connect("cen7.0.db", timeout=30)

sqlite3_cursor_handle=sqlite_connect_handle.cursor()
sqlite3_cursor_handle_relevance=sqlite_connect_handle_relevance.cursor()


def insertValue2Table(value):
    try:
        sqlite3_cursor_handle.execute(value)
        sqlite_connect_handle.commit()
    except sqlite3.IntegrityError as e:
        print("sqlite UNIQUE faild")


def createTable():
    sqlite3_cursor_handle.execute("CREATE TABLE IF NOT EXISTS `elf_rpmpackage_relevance` ( \
	`elfname`	TEXT, \
	`rpmpackagename`	TEXT, \
    `rpmname`	TEXT, \
	`osversion`	TEXT ,\
    PRIMARY KEY(`elfname`,`rpmpackagename`));")
    sqlite_connect_handle.commit()

    sqlite3_cursor_handle.execute("CREATE TABLE IF NOT EXISTS `lib_rpmpackage_relevance` ( \
	`libname`	TEXT, \
	`rpmpackagename`	TEXT, \
	`is_link`	INTEGER, \
	`link_file`	TEXT, \
    `rpmname`	TEXT, \
	`osversion`	TEXT, \
    PRIMARY KEY(`libname`,`rpmpackagename`));")
    sqlite_connect_handle.commit()

    # sqlite3_cursor_handle.execute("CREATE TABLE  IF NOT EXISTS `lib_symbol_diff` ( \
	# `cen_libname`	TEXT, \
	# `cq_libname`	TEXT, \
	# `symbol_name`	TEXT, \
	# `effect_type`	TEXT, \
	# `effect_result`	TEXT, \
    # `effect_file`	TEXT,\
	# `verdict`	INTEGER,\
    # PRIMARY KEY(`cen_libname`,`cq_libname`,`symbol_name`));")
    # sqlite_connect_handle.commit()


def insertLibRpmPakcageRelevanceValue2Table(libname,rpmpackgename,is_link,link_file,rpmname,osverion):
    sql="insert into lib_rpmpackage_relevance values('{}','{}',{},'{}','{}','{}')".format(libname,rpmpackgename,is_link,link_file,rpmname,osverion)
    print(sql)
    insertValue2Table(sql)

def insertLibSymbolDiffValue2Table(elfname,rpmpackagename,rpmname,osversion):
    sql="insert into elf_rpmpackage_relevance values('{}','{}','{}','{}')".format(elfname,rpmpackagename,rpmname,osversion)
    print(sql)
    insertValue2Table(sql)
    
def insertOtherFilesToTable(filename,rpmpackagename,rpmname,osversion):
    sql="insert into elf_rpmpackage_relevance values('{}','{}','{}','{}')".format(filename,rpmpackagename,rpmname,osversion)
    print(sql)
    insertValue2Table(sql)